﻿#include "qslog_table_model.h"

#pragma execution_character_set("utf-8")

using namespace QsLogging;

QsLogTableModel::QsLogTableModel(QObject *parent)
    : QAbstractTableModel(parent)
{
    Logger& logger = Logger::instance();
    DestinationPtr sigsSlotDestination(DestinationFactory::MakeFunctorDestination(this, SLOT(logSlot(QString,int))));
    logger.addDestination(sigsSlotDestination);

    maxStoreMessages = 1024;
    maxDisMessages = 128;
    disLevel = 0;
}

QVariant QsLogTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation==Qt::Horizontal && role==Qt::DisplayRole)
    {
        switch (section) {
        case 0:
            return "时间戳";
            break;
        case 1:
            return "内容";//"等级";
            break;
//        case 2:
//            return "内容";
//            break;
        default:
            break;
        }
    }
    else if(orientation==Qt::Horizontal && role==Qt::TextAlignmentRole)
    {

        return int(Qt::AlignLeft | Qt::AlignVCenter);
    }
    return QVariant();
}

int QsLogTableModel::rowCount(const QModelIndex &parent) const
{
    if(disLevel == QsLogging::InfoLevel)
    {
        if(infoMessages.count() > maxDisMessages)
        {
            return maxDisMessages;
        }
        else
            return infoMessages.count();
    }
    else if(disLevel == QsLogging::WarnLevel)
    {
        if(warnMessages.count() > maxDisMessages)
        {
            return maxDisMessages;
        }
        else
            return warnMessages.count();
    }
    else if(disLevel == QsLogging::FatalLevel)
    {
        if(fatalMessages.count() > maxDisMessages)
        {
            return maxDisMessages;
        }
        else
            return fatalMessages.count();
    }
    else
    {
        if(logMessages.count() > maxDisMessages)
        {
            return maxDisMessages;
        }
        else
            return logMessages.count();
    }

}

int QsLogTableModel::columnCount(const QModelIndex &parent) const
{
    return 2;
}

QVariant QsLogTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    QStringList strResult;
    if(role == Qt::DisplayRole)
    {
        int row = index.row();

        if(disLevel == QsLogging::InfoLevel)
        {
            if(row < infoMessages.count())
            {
                strResult = infoMessages.at(row).split("$");
            }
        }
        else if(disLevel == QsLogging::WarnLevel)
        {
            if(row < warnMessages.count())
            {
                strResult = warnMessages.at(row).split("$");
            }
        }
        else if(disLevel == QsLogging::FatalLevel)
        {
            if(row < fatalMessages.count())
            {
                strResult = fatalMessages.at(row).split("$");
            }
        }
        else
        {
            if(row < logMessages.count())
            {
                strResult = logMessages.at(row).split("$");
            }
        }
        if(strResult.count() >= 3)
        {
            if(0 == index.column())
            {
//                if(strResult.count() == 4)
//                {
//                    return strResult.at(0) + strResult.at(2);
//                }
//                return strResult.at(0);
                QString strTime = strResult.at(1);
                if(!strTime.isEmpty())
                {
                    strTime = strTime.replace('T',' ');
//                    return strTime;
                }
                return strResult.at(0) + strTime;
            }
            else if(1 == index.column())
            {
                if(strResult.count() == 4)
                {
                    return strResult.at(3);
                }
                return strResult.at(2);
            }
            else if(2 == index.column())
            {
//                QString strTime = strResult.at(1);
//                if(!strTime.isEmpty())
//                {
//                    strTime = strTime.replace('T',' ');
//                    return strTime;
//                }
//                return strResult.at(1);

                if(strResult.count() == 4)
                {
                    return strResult.at(0) + strResult.at(2);
                }
                return strResult.at(0);
            }
        }
    }
    else if(role == Qt::TextAlignmentRole)
    {
        if(0 == index.column())
        {
            return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
        }
        else
        {
            return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
        }
    }
    return QVariant();
}
void QsLogTableModel::logSlot(const QString &message, int level)
{
    if(logMessages.count() >= maxStoreMessages)
    {
        logMessages.removeLast();
    }
    logMessages.push_front(message);

    if(level == QsLogging::InfoLevel)
    {
        if(infoMessages.count() >= maxStoreMessages)
        {
            infoMessages.removeLast();
        }
        infoMessages.push_front(message);
    }
    else if(level == QsLogging::WarnLevel)
    {
        if(warnMessages.count() >= maxStoreMessages)
        {
            warnMessages.removeLast();
        }
        warnMessages.push_front(message);
    }
    else if(level == QsLogging::FatalLevel)
    {
        if(fatalMessages.count() >= maxStoreMessages)
        {
            fatalMessages.removeLast();
        }
        fatalMessages.push_front(message);
    }
    beginResetModel();
    endResetModel();
//        beginInsertRows(QModelIndex(),logMessages.count() - 1,logMessages.count() - 1);
//        endInsertRows();
}
void QsLogTableModel::setHistoryData(QStringList& his)
{
    foreach(auto value,his)
    {
        QsLogging::Level level =  Logger::levelFromLogMessage(value);
        logSlot(value,level);
    }
}

void QsLogTableModel::clearDisLevel(int level)
{
    if(level == QsLogging::DebugLevel)
    {
        logMessages.clear();
    }
    else if(level == QsLogging::InfoLevel)
    {
        infoMessages.clear();
    }
    else if(level == QsLogging::WarnLevel)
    {
        warnMessages.clear();
    }
    else if(level == QsLogging::FatalLevel)
    {
        fatalMessages.clear();
    }


    if(level != QsLogging::DebugLevel)
    {

    }
    beginResetModel();
    endResetModel();
}
void QsLogTableModel::setDisLevel(int l)
{
    disLevel = l;
    beginResetModel();
    endResetModel();
}
